package com.awen.htmlparser.parser;

import com.awen.htmlparser.Exception.ParseException;
import com.awen.htmlparser.rule.CyouHtmlParseRule;
import com.awen.htmlparser.rule.CyouHtmlParseRuleItem;
import com.awen.htmlparser.rule.RuleConfig;
import com.awen.htmlparser.util.PlainTextProcessUtil;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

/**
 * 畅游17173爬虫解析器
 * User: awen
 * Date: 13-4-8
 * Time: 上午9:33
 * To change this template use File | Settings | File Templates.
 */
public abstract class AbstractCyouParser {

    /**
     * 根据配置的format格式解析element值value
     *
     * @param plainTextFormat
     * @param value
     * @return
     */
    protected static String parsePlainText(String plainTextFormat, String value) {
        String plainTextType = plainTextFormat.split("[:]")[0];
        String[] args = (plainTextFormat.contains(":") && !plainTextFormat.endsWith(":")) ?
                plainTextFormat.split("[:]")[1].split(",") : new String[0];
        return PlainTextProcessUtil.process(plainTextType, value, args);
    }

    /**
     * 对cyouHtmlParseRuleItem通过读取ruleConfig配置（nodeFilter，index等）对当前htmlText进行解析
     *
     * @param ruleConfig
     * @param htmlText
     * @param encoding
     * @param plainTextFormat
     * @return
     * @throws ParserException
     */
    protected static String parseItemHtml(RuleConfig ruleConfig, String htmlText, String encoding, String plainTextFormat) throws ParserException, ParseException {

        Parser parser = new Parser();
        parser.setInputHTML(htmlText);
        parser.setEncoding(encoding);

        NodeList nodeList = parser.parse(ruleConfig.getNodeFilter());
        if (nodeList == null || nodeList.size() == 0) {
            //throw new ParseException("htmlText不能匹配" + ruleConfig.getPathPattern() + "Pattern");
            return "";
        }
        if (ruleConfig.getChild() != null) {
            return parseItemHtml(ruleConfig.getChild(), nodeList.elementAt(ruleConfig.getIndex()).toHtml().trim(), encoding, plainTextFormat);
        } else {
            return parsePlainText(plainTextFormat, nodeList.elementAt(ruleConfig.getIndex()).toPlainTextString().trim());
        }


    }

    /**
     * 对cyouHtmlParseRuleItem通过读取ruleConfig配置（nodeFilter，index等）对当前htmlText进行解析
     *
     * @param cyouHtmlParseRuleItem
     * @param ruleConfig
     * @param htmlText
     * @return
     * @throws ParserException
     */
    protected static String parseItemHtml(CyouHtmlParseRuleItem cyouHtmlParseRuleItem, RuleConfig ruleConfig, String htmlText) throws ParserException, ParseException {
        Parser parser = new Parser();
        parser.setInputHTML(htmlText);
        parser.setEncoding(cyouHtmlParseRuleItem.getEncoding());
        NodeList nodeList = parser.parse(ruleConfig.getNodeFilter());
        if (nodeList == null || nodeList.size() == 0) {
            //throw new ParseException("htmlText不能匹配" + ruleConfig.getPathPattern() + "Pattern");
            return "";
        }
        if (ruleConfig.getChild() != null) {
            return parseItemHtml(cyouHtmlParseRuleItem, ruleConfig.getChild(), nodeList.elementAt(ruleConfig.getIndex()).toHtml().trim());
        } else {
            return parsePlainText(cyouHtmlParseRuleItem.getPlainTextFormat(), nodeList.elementAt(ruleConfig.getIndex()).toPlainTextString().trim());
        }
    }

    /**
     * 对cyouHtmlParseRule通过读取ruleConfig配置（nodeFilter，index等）对当前htmlText进行解析
     *
     * @param cyouHtmlParseRule
     * @param ruleConfig
     * @param htmlText
     * @return
     * @throws ParserException
     */
    protected static String parseItemHtml(CyouHtmlParseRule cyouHtmlParseRule, RuleConfig ruleConfig, String htmlText) throws ParserException, ParseException {
        Parser parser = new Parser();
        parser.setInputHTML(htmlText);
        parser.setEncoding(cyouHtmlParseRule.getEncoding());
        NodeList nodeList = parser.parse(ruleConfig.getNodeFilter());
        if (nodeList == null || nodeList.size() == 0) {
            //throw new ParseException("htmlText不能匹配" + ruleConfig.getPathPattern() + "Pattern");
            return "";
        }
        if (ruleConfig.getChild() != null) {
            return parseItemHtml(cyouHtmlParseRule, ruleConfig.getChild(), nodeList.elementAt(ruleConfig.getIndex()).toHtml().trim());
        } else {
            return parsePlainText(cyouHtmlParseRule.getPlainTextFormat(), nodeList.elementAt(ruleConfig.getIndex()).toPlainTextString().trim());
        }
    }

}

